var app = getApp();

Page({
    data: {
        navH: 0,
        goodsList: !1,
        page: 1,
        lastPage: !1,
        loading: !1,
        taskList: !1,
        uInfo: !1,
        videoAd: !1,
        appletKeyword: "",
        showtipOr: !1
    },
    viewVideoAd: function() {
        this.data.videoAd && this.data.videoAd.show();
    },
    initVideoAd: function() {
        var t = this;
        !1 === t.data.videoAd && wx.createRewardedVideoAd && app.globalData.setting.video_ad_code && (t.data.videoAd = wx.createRewardedVideoAd({
            adUnitId: "adunit-" + app.globalData.setting.video_ad_code
        }), t.data.videoAd.onLoad(function() {
            console.log("Video_AD Load OK...");
        }), t.data.videoAd.onClose(function(a) {
            console.log("Video_AD Close OK ..."), console.log(a), a && a.isEnded || void 0 === a ? (console.log("watch OK ..."), 
            app.util.request({
                url: "entry/wxapp/watchVideoRewards",
                success: function(a) {
                    wx.showToast({
                        title: "领取成功",
                        icon: "success"
                    }), app.checkLogin(), t.getTaskList();
                }
            })) : (console.log("noWatch OK ..."), wx.showModal({
                title: "提示",
                content: "视频没看完，看完视频才能得到任务奖励哦~~~",
                showCancel: !0,
                confirmColor: "#f43f6c",
                confirmText: "继续观看",
                cancelColor: "#bbb",
                success: function(a) {
                    a.confirm && t.data.videoAd && t.data.videoAd.show();
                },
                fail: function(a) {
                    console.log(a, "showModalErr");
                }
            }));
        }), t.data.videoAd.onError(function(a) {
            console.log(a, "VieoERR....");
        }));
    },
    getRunStep: function() {
        var t = this;
        wx.getWeRunData({
            success: function(a) {
                a.iv && app.util.request({
                    url: "entry/wxapp/EncryptRunStep",
                    data: {
                        iv: a.iv,
                        encryptedData: a.encryptedData
                    },
                    method: "POST",
                    success: function(a) {
                        wx.showToast({
                            title: "领取成功",
                            icon: "success"
                        }), t.disTaskByLabel(6), app.checkLogin();
                    }
                });
            }
        });
    },
    signIn: function() {
        var t = this;
        app.util.request({
            url: "entry/wxapp/signIn",
            success: function(a) {
                wx.showToast({
                    title: "签到成功",
                    icon: "success"
                }), t.disTaskByLabel(2), app.checkLogin();
            }
        });
    },
    toPageTop: function() {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 0
        });
    },
    coinExCash: function() {
        app.globalData.userInfo ? app.util.request({
            url: "entry/wxapp/coinExCash",
            success: function(a) {
                wx.showToast({
                    title: "兑换成功",
                    icon: "success"
                }), app.checkLogin();
            }
        }) : app.checkLogin();
    },
    disTaskByLabel: function(a) {
        var t, o = this.data.taskList;
        for (t in o) o[t].label == a && (o[t].dis = !0);
        this.setData({
            taskList: o
        });
    },
    showInfo: function() {
        app.showInfo(this, app.globalData.setting.task_rule_intro);
    },
    hideInfo: function() {
        app.hideInfo(this);
    },
    toCoupon: function() {
        wx.navigateTo({
            url: "../coupon/coupon"
        });
    },
    toInvite: function() {
        wx.navigateTo({
            url: "../invite/invite"
        });
    },
    toPartake: function() {
        wx.navigateTo({
            url: "../partake/partake"
        });
    },
    goIndex: function() {
        wx.switchTab({
            url: "../index/index"
        });
    },
    goRedeem: function() {
        wx.navigateTo({
            url: "../redeem/redeem"
        });
    },
    goRules: function() {
        wx.navigateTo({
            url: "./rules"
        });
    },
    toCash: function() {
        app.globalData.userInfo ? wx.navigateTo({
            url: "../cash/cash"
        }) : app.checkLogin();
    },
    sharetoFq: function() {
        app.checkLogin();
    },
    onLoad: function(a) {
        this.setData({
            navH: app.globalData.navHeight,
            appletKeyword: app.globalData.setting.appletKeyword,
            withdraw_open: app.globalData.setting.withdraw_open
        });
        var t = !!app.globalData.userInfo;
        this.setData({
            isLogin: t
        }), this.thisNav(), this.getRcommendGoods();
    },
    thisNav: function() {
        var o = this;
        app.util.request({
            url: "entry/wxapp/getBottomNav",
            success: function(a) {
                console.log(a.data.data);
                var t = a.data.data;
                void 0 !== t.list.length && t.list.length < 1 ? wx.setTabBarStyle({
                    color: t.color,
                    selectedColor: t.selectedColor,
                    backgroundColor: t.backgroundColor,
                    borderStyle: t.borderStyle,
                    fail: function(a) {
                        console.log(a);
                    }
                }) : (wx.hideTabBar(), o.setData({
                    tabBar: a.data.data,
                    "tabBar.thisurl": o.__route__
                }));
            }
        });
    },
    getTaskList: function(t) {
        var o = this;
        app.util.request({
            url: "entry/wxapp/getTaskList",
            showLoading: "function" == typeof t,
            success: function(a) {
                "function" == typeof t && (t(), app.checkLogin()), o.setData({
                    taskList: a.data.data,
                    taskLen: a.data.data.length
                }, function() {
                    for (var a in o.data.taskList) if (5 == o.data.taskList[a].label) {
                        "devtools" != app.platform && o.initVideoAd();
                        break;
                    }
                });
            }
        });
    },
    loginAfter: function(a) {
        app.loginAfter(a);
    },
    loginCancel: function() {
        app.loginCancel();
    },
    getRcommendGoods: function() {
        var t = this;
        app.util.request({
            url: "entry/wxapp/getRcommendGoods",
            showLoading: !(t.data.page < 2),
            data: {
                page: t.data.page
            },
            success: function(a) {
                console.log(a), a.data.data.length < 1 ? t.data.lastPage = !0 : (t.data.goodsList = 1 == t.data.page ? a.data.data : t.data.goodsList.concat(a.data.data), 
                t.setData({
                    goodsList: t.data.goodsList
                }), t.data.loading = !1);
            }
        });
    },
    toGoodsPage: function(a) {
        var t = a.currentTarget.dataset.goods_id;
        void 0 !== a.currentTarget.dataset.goods_sign && (t += "&goods_sign=" + a.currentTarget.dataset.goods_sign), 
        wx.navigateTo({
            url: "../goods/goods?goods_id=" + t
        });
    },
    onReady: function() {},
    backTop: function(a) {
        wx.pageScrollTo ? wx.pageScrollTo({
            scrollTop: 0
        }) : console.log("当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。");
    },
    onShow: function() {
        app.checkLogin(), this.getTaskList();
    },
    confirmSmart: function() {
        app.confirmSmart(this);
    },
    hideSmart: function() {
        app.hideSmart(this);
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {
        this.getTaskList(function() {
            wx.stopPullDownRefresh();
        });
    },
    onReachBottom: function() {
        var a = this;
        !0 !== a.data.lastPage && !0 !== a.data.loading && (a.data.page++, a.data.loading = !0, 
        a.getRcommendGoods());
    },
    onShareAppMessage: function() {
        var a = app.globalData.setting.share_title || "购物省钱，分享赚钱", t = app.globalData.setting.share_pic;
        return {
            title: a,
            path: "/first_duoduoke/pages/index/index" + (void 0 !== app.globalData.userInfo.uid ? "?from_uid=" + app.globalData.userInfo.uid + "&from_act=share" : ""),
            imageUrl: t,
            success: function(a) {
                "shareAppMessage:ok" == a.errMsg ? wx.showToast({
                    title: "分享成功",
                    icon: "success"
                }) : wx.showToast({
                    title: "分享失败",
                    icon: "none"
                });
            },
            fail: function(a) {
                "shareAppMessage:fail cancel" == a.errMsg || a.errMsg, wx.showToast({
                    title: "分享失败",
                    icon: "none"
                });
            }
        };
    }
});